package com.sz.business.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import com.sz.business.bmo.IBusinessBMO;

import org.springframework.web.servlet.mvc.Controller;

public class AddBusiness extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {
			
			String rtn_type = request.getParameter("return_type");
			String path = request.getContextPath(); 
			
			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			IBusinessBMO businessBMO = (IBusinessBMO) context.getBean("BusinessBMO");

			Map map = new HashMap();
			
			String Business_Name 	  = request.getParameter("Business_Name");
			String Business_Tel1		  = request.getParameter("Business_Tel1");
			String Business_Group_ID  = request.getParameter("business_group_id");
			String Business_Region_ID = request.getParameter("business_region_id");
			String Business_InfoSource = request.getParameter("Business_InfoSource");
			String Business_CheckStatus = request.getParameter("Business_CheckStatus");
			String Business_WorkStatus = request.getParameter("Business_WorkStatus");
			String Business_PreorderFlag = request.getParameter("Business_PreorderFlag");
			String Business_PublishState = request.getParameter("Business_PublishState");
			String Business_RedFlag = request.getParameter("Business_RedFlag");
			String Business_X = request.getParameter("Business_X");
			String Business_Y = request.getParameter("Business_Y");
			String Business_NameEnglish = request.getParameter("Business_NameEnglish");
			String Business_Alias = request.getParameter("Business_Alias");
			String Business_LoginName = request.getParameter("Business_LoginName");
			String Business_Adress = request.getParameter("Business_Adress");
			String Business_PostCode = request.getParameter("Business_PostCode");
			String Business_CrossRoad = request.getParameter("Business_CrossRoad");
			String Business_LandMark = request.getParameter("Business_LandMark");
			String Business_Traffic = request.getParameter("Business_Traffic");
			String Business_Descript = request.getParameter("Business_Descript");
			String Business_Email = request.getParameter("Business_Email");
			String Business_NetAddress = request.getParameter("Business_NetAddress");
			String Business_Fax = request.getParameter("Business_Fax");
			String Business_MainUser = request.getParameter("Business_MainUser");
			String Business_MainUser_Tel = request.getParameter("Business_MainUser_Tel");
			String Business_BusinessCode = request.getParameter("Business_BusinessCode");
			
			String Business_RoomDescrip = request.getParameter("Business_RoomDescrip");//包厢描述

			if(Business_BusinessCode != null) {
				//从数据上限制为大写
				Business_BusinessCode = Business_BusinessCode.toUpperCase();
			}
			
			
			
			
			
			
			String course_serial_id = request.getParameter("course_serial_id");
			String Business_SpecialCourse = request.getParameter("Business_SpecialCourse");
			String Business_Flavor = request.getParameter("Business_Flavor");
			
			String Business_AvgFee = request.getParameter("Business_AvgFee");
			String Business_LowFee = request.getParameter("Business_LowFee");
			String Business_ServiceFee = request.getParameter("Business_ServiceFee");
			String Business_Discount = request.getParameter("Business_Discount");
			
			String Business_RoomFlag = request.getParameter("Business_RoomFlag");
			String Business_PreorderDescrip = request.getParameter("Business_PreorderDescrip");
			String Business_Reserve = request.getParameter("Business_Reserve");
			
			String Business_WorkTime = request.getParameter("Business_WorkTime");
			String Business_RoomNum = request.getParameter("Business_RoomNum");
			String Business_Devices = request.getParameter("Business_Devices");
		    
			String Business_CreditCardFlag = request.getParameter("Business_CreditCardFlag");
			String Business_CousumeCardFlag = request.getParameter("Business_CousumeCardFlag");
			String Business_CardTypeInfo = request.getParameter("Business_CardTypeInfo");
		    
			String Business_ServiceDescrip = request.getParameter("Business_ServiceDescrip");
			String Business_ParkDescrip = request.getParameter("Business_ParkDescrip");
			String Business_OutDescrip = request.getParameter("Business_OutDescrip");
		    
			String Business_Area = request.getParameter("Business_Area");
			String Business_Environment = request.getParameter("Business_Environment");
		    
			String Business_KeyWords = request.getParameter("Business_KeyWords");
			String Business_CommentText = request.getParameter("Business_CommentText");
			String Business_DepositDescrip = request.getParameter("Business_DepositDescrip");
			
			String Business_CheckOrderMode = request.getParameter("Business_CheckOrderMode");
			String Business_CheckContact = request.getParameter("Business_CheckContact");
			String Business_CheckTel = request.getParameter("Business_CheckTel");
		    
		    
			
			String create_manager = request.getSession().getAttribute("username").toString();
			
			map.put("GROUP_ID", Business_Group_ID);
			map.put("REGION_ID", Business_Region_ID);
			map.put("INFO_SOURCE",Business_InfoSource);

			map.put("CHECK_STATUS", Business_CheckStatus);
			map.put("WORK_STATUS", Business_WorkStatus);
			map.put("PREORDER_FLAG",Business_PreorderFlag);
			
			map.put("PUBLISH_STATE", Business_PublishState);
			map.put("RED_FLAG", Business_RedFlag);
			map.put("X",Business_X);
			map.put("Y",Business_Y);

			map.put("NAME", Business_Name);
			map.put("NAME_ENGLISH", Business_NameEnglish);
			map.put("ALIAS",Business_Alias);
			
			map.put("LOGIN_NAME", Business_LoginName);
			map.put("TEL1", Business_Tel1);
			map.put("ADDRESS",Business_Adress);
         
			map.put("POST_CODE", Business_PostCode);
			map.put("CROSSROAD", Business_CrossRoad);
			map.put("LANDMARK", Business_LandMark);
			
			map.put("TRAFFIC", Business_Traffic);
			map.put("DESCRIPT", Business_Descript);
			
			map.put("EMAIL", Business_Email);
			map.put("NET_ADDRESS", Business_NetAddress);
			map.put("FAX", Business_Fax);
			
			map.put("MAIN_USER", Business_MainUser);
			map.put("MAIN_USER_TEL", Business_MainUser_Tel);
			map.put("BUSINESS_CODE", Business_BusinessCode);
			map.put("ROOM_DESCRIP", Business_RoomDescrip);
			
			
			map.put("MAIN_SERIAL", course_serial_id);
		    map.put("SPECIAL_COURSE", Business_SpecialCourse);
			map.put("FLAVOUR", Business_Flavor);
			map.put("AVG_FEE",Business_AvgFee);
		    map.put("LOW_FEE",Business_LowFee);
		    map.put("SERVICE_FEE", Business_ServiceFee);
			map.put("DISCOUNT",Business_Discount);               
			map.put("ROOM_FLAG",Business_RoomFlag);
		    map.put("PREORDER_DESCRIP",Business_PreorderDescrip);
			map.put("RESERVE",Business_Reserve);			
			map.put("WORKTIME",Business_WorkTime);
		    map.put("ROOM_NUM",Business_RoomNum);
			map.put("DEVICES",Business_Devices);			
			map.put("CREDITCARD_FLAG",Business_CreditCardFlag);
		    map.put("CONSUMECARD_FLAG",Business_CousumeCardFlag);
			map.put("CARD_TYPE_INFO",Business_CardTypeInfo);
			map.put("SERVICE_DESCRIP",Business_ServiceDescrip);
			map.put("PARK_DESCRIP",Business_ParkDescrip);
			map.put("OUT_DESCRIP",Business_OutDescrip);
			map.put("AREA",Business_Area);
			map.put("ENVIRONMENT",Business_Environment);
			map.put("KEYWORDS",Business_KeyWords);
			map.put("COMMENT_TEXT",Business_CommentText);
			map.put("DEPOSIT_DESCRIP",Business_DepositDescrip);
			
			map.put("CHECK_ORDER_MODE", Business_CheckOrderMode);
			map.put("CHECK_CONTACT", Business_CheckContact);
			map.put("CHECK_TEL", Business_CheckTel);
			
			

			map.put("INPUT_USER", create_manager);
			
			long r = businessBMO.addBusiness(map);
			logger.info(String.format("addBusiness:%d", r));			

			if(r > 0) {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("成功！");
				} else {
					response.sendRedirect(path + "/page/success.jsp");
				}
			} else {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("失败");
				} else {
					response.sendRedirect(path + "/page/failure.jsp");

				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}
}
